﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace DBManager.Admin
{
    public class Company
    {
        public static DataTable GetAllCompanyByStatus(string status)
        {
            //thong tin 
            string sql = "select * from companies where Status=" + comm.to_sqlnumber(status)+" order by status asc";
            return DBUtil.SelectTable(sql);
        }
        public static DataTable GetInfomation(string companyID)
        {
            //thong tin 
            string sql = "select * from companies where companyID=" + comm.to_sqlnumber(companyID);
            return DBUtil.SelectTable(sql);
        }
        public static DataTable GetAllCompany(string search, string checkStatus)
        {
            //show tat ca
            string dkStatus = "";
            if (checkStatus != "2")
            {
                dkStatus = " and Status=" + comm.to_sqlnumber(checkStatus) + " ";
            }
            string dk = "";
            if (search != "")
            {
                dk = " and NameCp like N'%" + search + "%' ";
            }
            string sql = "select *,case status when 1 then N'Sử dụng' when 0 then N'Không dùng' end as trangthai from companies where 1=1" + dk + dkStatus + " order by companyid desc";
            return DBUtil.SelectTable(sql);
        }
        public static int EditCompany(string NameCp, string Website, string Status, string Note, string Email, string Logo, string companyID)
        {
            //sua cong ty
            int kq = 0;
            try
            {
                string sql = @"UPDATE Companies 
                              SET NameCp = " + comm.to_sqltext(NameCp) + @"
                              ,Website = " + comm.to_sqltext(Website) + @"
                              ,Status = " + comm.to_sqlnumber(Status) + @"
                              ,Note = " + comm.to_sqltext(Note) + @"
                              ,Email = " + comm.to_sqltext(Email) + @"
                              ,Logo = " + comm.to_sqltext(Logo) + @"
                              WHERE companyid=" + comm.to_sqlnumber(companyID);
                DBUtil.Execute(sql);
                kq = 1;
            }
            catch (Exception)
            {
                kq = 0;
            }
            return kq;
        }
        public static int AddCompany(string NameCp, string Website, string Status, string Note, string Email, string Logo)
        {
            //them cong ty
            int kq = 0;
            try
            {
                string sql = @"INSERT INTO Companies  
           (NameCp,Website,Status,Note,Email,Logo) 
            VALUES
           (" + comm.to_sqltext(NameCp) + @"
           ," + comm.to_sqltext(Website) + @"
           ," + comm.to_sqlnumber(Status) + @"
           ," + comm.to_sqltext(Note) + @"
           ," + comm.to_sqltext(Email) + @"
           ," + comm.to_sqltext(Logo) + ")";
                DBUtil.Execute(sql);
                kq = 1;
            }
            catch (Exception)
            {
                kq = 0;
            }
            return kq;
        }
    }
}
